package com.example.module_hp_white_noise.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;

import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.module_hp_white_noise.R;
import com.example.module_hp_white_noise.activity.HpWhiteNoiseChildListActivity;
import com.example.module_hp_white_noise.activity.HpWhiteNoiseChildPlayActivity;
import com.example.module_hp_white_noise.adapter.HpWhiteNoiseFragment1ChildAdapter;
import com.example.module_hp_white_noise.adapter.HpWhiteNoisePage1Adapter;
import com.example.module_hp_white_noise.adapter.HpWhiteNoisePage1Chunk2Adapter;
import com.example.module_hp_white_noise.base.BaseMvvmFragment;
import com.example.module_hp_white_noise.base.BaseUtils;
import com.example.module_hp_white_noise.base.BaseViewModel;
import com.example.module_hp_white_noise.databinding.FragmentHpWhiteNoisePage1Binding;
import com.example.module_hp_white_noise.databinding.FragmentHpWhiteNoisePage3Binding;
import com.example.module_hp_white_noise.entity.HpWhiteNoiseEntity1;
import com.example.module_hp_white_noise.util.Utils;
import com.fwlst.lib_base.utils.AdUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class HpWhiteNoiseFragment3 extends BaseMvvmFragment<FragmentHpWhiteNoisePage3Binding, BaseViewModel> {
    private HpWhiteNoiseFragment1ChildAdapter hpWhiteNoiseFragment1ChildAdapter;
    private HpWhiteNoisePage1Adapter hpWhiteNoisePage1Adapter;
    private HpWhiteNoisePage1Chunk2Adapter hpWhiteNoisePage1Chunk2Adapter;
    private List<String> mDataNameList;
    private List<Integer> mDataTagList;
    private List<HpWhiteNoiseEntity1> mDataList1;
    private List<HpWhiteNoiseEntity1> mDataList2;
    private JSONArray jsonArray;
    private JSONArray mDataArray;
    @Override
    protected int initLayout() {
        return R.layout.fragment_hp_white_noise_page3;
    }

    @Override
    protected int initBR() {
        return 0;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        //banner广告
        AdUtils.INSTANCE.loadBannerAd(mContext,binding.bannerContainer);
        //顶部切换list
        hpWhiteNoiseFragment1ChildAdapter = new HpWhiteNoiseFragment1ChildAdapter();
        binding.fragment3Chunk1Rv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL,false));
        binding.fragment3Chunk1Rv.setAdapter(hpWhiteNoiseFragment1ChildAdapter);
        hpWhiteNoiseFragment1ChildAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                hpWhiteNoiseFragment1ChildAdapter.position = position;
                hpWhiteNoiseFragment1ChildAdapter.setNewData(mDataNameList);
                setDataList(position);
            }
        });
        //中间list
        hpWhiteNoisePage1Adapter = new HpWhiteNoisePage1Adapter();
        binding.fragment3Chunk2Rv.setLayoutManager(new GridLayoutManager(mContext,2));
        binding.fragment3Chunk2Rv.setAdapter(hpWhiteNoisePage1Adapter);
        hpWhiteNoisePage1Adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bd_item = new Bundle();
                bd_item.putString("songUrl",mDataList1.get(position).getMusicUrl());
                bd_item.putString("imgUrl",mDataList1.get(position).getImgUrl());
                bd_item.putString("title",mDataList1.get(position).getTitle());
                navigateToWithBundle(HpWhiteNoiseChildPlayActivity.class,bd_item);
            }
        });
        //底部list
        hpWhiteNoisePage1Chunk2Adapter = new HpWhiteNoisePage1Chunk2Adapter();
        binding.fragment3Chunk3Rv.setLayoutManager(new LinearLayoutManager(mContext));
        binding.fragment3Chunk3Rv.setAdapter(hpWhiteNoisePage1Chunk2Adapter);
        hpWhiteNoisePage1Chunk2Adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bd_item = new Bundle();
                bd_item.putString("songUrl",mDataList2.get(position).getMusicUrl());
                bd_item.putString("imgUrl",mDataList2.get(position).getImgUrl());
                bd_item.putString("title",mDataList2.get(position).getTitle());
                navigateToWithBundle(HpWhiteNoiseChildPlayActivity.class,bd_item);
            }
        });
        initData();
    }
    //加载数据
    private void initData(){
        mDataNameList = new ArrayList<>();
        mDataTagList = new ArrayList<>();
        String objString = BaseUtils.getJson("baizaoying.json",mContext);
        String objClassify = BaseUtils.getJson("classify.json",mContext);
        try {
            jsonArray = new JSONArray(objString);
            JSONArray classifyArr = new JSONArray(objClassify);
            JSONObject classifyData = (JSONObject) classifyArr.get(3);
            mDataArray = classifyData.getJSONArray("classify");
            int arrLength = mDataArray.length();
            for(int i=0;i<arrLength;i++){
                JSONObject jsonObject = (JSONObject) mDataArray.get(i);
                mDataNameList.add(jsonObject.getString("name"));
                mDataTagList.add(jsonObject.getInt("tags"));
            }
            if(arrLength>0){
                setDataList(0);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        hpWhiteNoiseFragment1ChildAdapter.setNewData(mDataNameList);
    }
    //设置底部list
    private void setDataList(int index){
        if(jsonArray!=null){
            try {
                int tags = mDataTagList.get(index);
                int start = (tags-1)*10;
                mDataList1 = new ArrayList<>();
                mDataList2 = new ArrayList<>();
                for (int i=start;i<start+10;i++){
                    JSONObject jsonObject2 = (JSONObject) jsonArray.get(i);
                    HpWhiteNoiseEntity1 hpWhiteNoiseEntity1 = new HpWhiteNoiseEntity1(i,2,
                            //jsonObject2.getString("imgUrl"),
                            "music_img"+(i% Utils.musicImgCount+1),
                            jsonObject2.getString("ossId"),
                            jsonObject2.getString("name"),
                            "",
                            null
                    );
                    if(i==start||i==start+1){
                        mDataList1.add(hpWhiteNoiseEntity1);
                    }else {
                        mDataList2.add(hpWhiteNoiseEntity1);
                    }
                }
                //Log.e("mDataList",mDataList.toString());
                hpWhiteNoisePage1Adapter.setNewData(mDataList1);
                hpWhiteNoisePage1Chunk2Adapter.setNewData(mDataList2);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        AdUtils.INSTANCE.destroyBannerAd();
    }
}
